package cn.bdqn.controller.question;


import cn.bdqn.pojo.question.Category;
import cn.bdqn.pojo.question.Message;
import cn.bdqn.pojo.question.Question;
import cn.bdqn.service.question.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Message getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List data = categoryService.list();
        Message message = new Message();
        message.setData(data);
        PageInfo<Question> page= new PageInfo<>(data);
        message.setPage(page);
        return message;
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/lev1")
    public List<Category> getAll11(){
        return categoryService.iSelectLev1();
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/lev2")
    public List<Category> getAll12(){
        return categoryService.iSelectLev2();
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/lev3")
    public List<Category> getAll13(){
        return categoryService.iSelectLev3();
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/lev1/{pageNum}/{pageSize}")
    public Message getAll1(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List data = categoryService.iSelectLev1();
        Message message = new Message();
        message.setData(data);
        PageInfo<Question> page= new PageInfo<>(data);
        message.setPage(page);
        return message;
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/lev2/{pageNum}/{pageSize}")
    public Message getAll12(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List data = categoryService.iSelectLev2();
        Message message = new Message();
        message.setData(data);
        PageInfo<Question> page= new PageInfo<>(data);
        message.setPage(page);
        return message;
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/lev3/{pageNum}/{pageSize}")
    public Message getAll13(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List data = categoryService.iSelectLev3();
        Message message = new Message();
        message.setData(data);
        PageInfo<Question> page= new PageInfo<>(data);
        message.setPage(page);
        return message;
    }
    // 添加
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/add")
    public boolean save(@RequestBody Category category){
        return categoryService.save(category) ;
    }

    // 更新
    @PreAuthorize("hasAuthority('question:management')")
    @PutMapping("/update")
    public boolean update(@RequestBody Category category){
        return categoryService.updateById(category);
    }
    // 删除byId
    @PreAuthorize("hasAuthority('question:management')")
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id){
        return  categoryService.removeById(id);
    }
    // 查询单个
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/list/{id}")
    public Category getById(@PathVariable Integer id){
        return categoryService.getById(id);
    }
    // 模糊查询
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/like/{pageNum}")
    public Message getLike(@RequestParam("queryName") String name,@PathVariable Integer pageNum){
        System.out.println(name);
        QueryWrapper<Category> lq = new QueryWrapper<>();
        lq.like("question_category_name",name);
        PageHelper.startPage(pageNum,7);
        List data = categoryService.list(lq);
        Message message = new Message();
        message.setData(data);
        PageInfo<Question> page= new PageInfo<>(data);
        message.setPage(page);
        return message;
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/getId")
    public int getId(@RequestParam("name") String name ){
        return categoryService.iSelectId(name);
    }
}
